package com.game.gameshoppingcenter.controller;


import com.game.gameshoppingcenter.dao.UsersRepository;
import com.game.gameshoppingcenter.entities.Users;
import com.game.gameshoppingcenter.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/User")
public class UsersController {
    @Autowired
    UsersService userService;
    @Autowired
    UsersRepository usersRepository;
    //跳转到用户登录界面
    @RequestMapping("/usertoLogin")
    public String tologin(Model model){

        return "Login/login";
    }
    @RequestMapping("/userlogin")
    //用户登录如果为空则刷新界面，不为空则跳转到主页
    public String login(Model model, HttpSession session, String name, String password, HttpServletRequest servletRequest){
        boolean blog=userService.getfindByNameAndPassword(servletRequest);
        System.out.println(blog);

        if(blog) {
            return  "Shoppingstore/shoppingstore";
        }else{
            model.addAttribute("errorMessage","用户名或密码错误！");
            return "Login/login";

        }

    }
    @RequestMapping("/usertoRegister")
    public String toRegister(Model model){
        Users users =new Users();
        model.addAttribute("users", users);
        return "Register/register";
    }
    @RequestMapping("/userregister")
    public String register(){
        return  "Login/login";
    }
    @PostMapping("/save")
    public String createAdmin (Model model, Users users){
             userService.save(users);


             return "redirect:/User/usertoLogin";
    }
}
